#include "midea_collection_dynamic.h"
#include <math.h>
#include <stdlib.h>
#include "../base_scroll_ui/midea_base_scroll_tool.h"
#include "../base_scroll_ui/midea_base_scroll_cell.h"

#include "midea_collection_view.h"

#include "../../pt_printk.h"


// collection视图UI懒加载方法
// load_count <= 0 则根据当前平均每页cell数量 动态加载
// 从当前scroll的滑动位置再向后增加2页距离
// 如果content_size < scroll + 2 * page_size
// 则动态加载更多UI
// 一次动态加载UI数量 = 当前每页展示的平均数量 （至少2个UI item）
void midea_collection_dynamic_update(midea_collection_view_single *collection, int32_t load_count)
{
    if (collection == NULL)
    {
        return;
    }
    lv_obj_t *obj = (lv_obj_t *)collection;
    uint32_t current_ui_count = lv_obj_get_child_cnt(obj);
    if (current_ui_count > collection->count)
    {
        // 删除多余 UI item
        debug_d("\n");
        midea_delete_child_from_index(obj, collection->count, true);
        return;
    }

    // 计算每一页的平均UI个数
    // 做为每次加载UI的个数
    uint32_t end_index = 0;
    if (load_count <= 0)
    {
        lv_point_t point;
        lv_obj_get_scroll_end(obj, &point);
        lv_coord_t content_size = 0; // 内容高度或宽度
        lv_coord_t display_size = 0; // 显示高度或宽度
        lv_coord_t scroll_pos = 0;
        // 检测位置
        lv_coord_t check_pos = 0;
        if (collection->direction & LV_DIR_VER)
        {
            scroll_pos = point.y;
            content_size = midea_obj_scroll_length(obj, true, scroll_pos);
            display_size = lv_obj_get_height(obj);
        }
        else
        {
            scroll_pos = point.x;
            content_size = midea_obj_scroll_length(obj, false, scroll_pos);
            display_size = lv_obj_get_width(obj);
        }
        check_pos = scroll_pos + display_size + (display_size >> 2);
        // 检测"删除"位置
        lv_coord_t check_del_pos = scroll_pos + display_size + display_size;
        // 如果内容长度少于检测scroll位置
        // 则本次需要增加cell
        if ((collection->dynamic_load_tag.load_tag & MIDEA_DYNAMIC_LOAD) && content_size < check_pos)
        {
            // debug_d("\n");
            // MIDEA_DYNAMIC_LOAD 满足动态加载设置
            end_index = (uint32_t)roundf((float)display_size / (float)content_size * (float)current_ui_count);
            end_index = LV_MAX(end_index, MIDEA_COLLECTON_LAZY_MIN_ITEM_COUNT);
            end_index = LV_MIN(end_index, 10);
        }
        else if ((collection->dynamic_load_tag.load_tag & MIDEA_DYNAMIC_FREE) && content_size > check_del_pos)
        {
            // MIDEA_DYNAMIC_FREE 满足动态释放设置
            // 如果内容长度大于check_del_pos
            // 则删除一定数量cell
            // 注意只删除cell不删除数据
            int32_t del_from_index = midea_obj_child_id_of_scroll_position(obj, collection->direction & LV_DIR_VER, scroll_pos, check_del_pos);
            if (del_from_index != -1)
            {
                // 删除del_from_index之后的cell
                debug_d("del_index=%d\n",del_from_index);
                midea_delete_child_from_index(obj, del_from_index, true);
                debug_d("\n");
            }
            return;
        }
        else
        {
            return;
        }
    }
    else
    {
        end_index = load_count;
    }

    end_index = LV_MIN(end_index + current_ui_count - 1, collection->count - 1);
    // 按索引范围刷新UI

    // debug_d("current_ui_count %d end_index %d \n", current_ui_count, end_index);

    update_midea_collection_with_range(collection, current_ui_count, end_index);
}
// 滑动回调
// 处理懒加载
void midea_collection_scroll_event(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *target     = lv_event_get_target(e);
    if(target == NULL){
        return;
    }

    switch(code){
    case LV_EVENT_PRESSED:
    {
        lv_event_send(lv_obj_get_parent(target),LV_EVENT_PRESSED,NULL);
        break;
    }
    case LV_EVENT_PRESSING:
    {
        lv_event_send(lv_obj_get_parent(target),LV_EVENT_PRESSING,NULL);
        break;
    }
    case LV_EVENT_RELEASED:
    {
        lv_event_send(lv_obj_get_parent(target),LV_EVENT_RELEASED,NULL);
        break;
    }
    default:break;
    }

    if (!(code == LV_EVENT_SCROLL_END || code == LV_EVENT_SCROLL_BEGIN || code == LV_EVENT_PRESSED))
    {
        //debug_d("err0\n");
        return;
    }

    // 如果已经加载完所有UI
    // 则不需要再检测以及实施懒加载
    midea_collection_view_single *collection = (midea_collection_view_single *)target;
    if (collection == NULL)
    {
        debug_d("err2\n");
        return;
    }
    if (collection->dynamic_load_tag.load_tag & MIDEA_DYNAMIC_NONE)
    {
        // 没有启用懒加载
        debug_d("err3\n");
        return;
    }
    midea_collection_dynamic_update(collection, -1);
}

// 按索引区间刷新
// 不能隔断式根据索引范围刷新
// 因为UI索引和数据索引一一对应
// 如果start_index前存在数据索引找不到对应的UI索引
// 说明前面的刷新方法调用产生了错误
// 需要更正start_index
void update_midea_collection_with_range(midea_collection_view_single *collection, int32_t start_index, int32_t end_index)
{
    if (collection == NULL)
    {
        return;
    }
    lv_obj_t *obj = (lv_obj_t *)collection;
    int32_t new_start = start_index;
    if (lv_obj_get_child_cnt(obj) < start_index)
    {
        new_start = lv_obj_get_child_cnt(obj);
    }
    int32_t index = 0;
    midea_collection_view_single_data *current = collection->collection_head;
    while (current != NULL)
    {
        // lv_obj_get_child_id
        // debug_d("new_start %d end_index %d \n", new_start, end_index);
        if (index >= new_start && index <= end_index)
        {
            // debug_d("index %d \n", index);
            lv_obj_t *cell = lv_obj_get_child(obj, index);
            if (cell == NULL)
            {
                // 增加
                // 这里add_cell的索引必然==index
                lv_obj_t *add_cell = add_midea_collection_item_ui(collection, index, current, true);
                if (add_cell != NULL)
                {
                    collection->install_collection(current->data, add_cell, index, current->ui_type);
                    cell = add_cell;
                }

                // debug_d("add_cell=%x\n",add_cell);
            }
            else
            {
                // 比较ui_type是否一致
                if (!midea_base_scroll_cell_is_same_ui_type(cell, current->ui_type))
                {
                    // 不一致则查找
                    lv_obj_t *search_cell = search_midea_collection_item_ui(collection, index, current);
                    if (search_cell != NULL)
                    {
                        // 交换
                        exchange_midea_collection_item_ui(collection, cell, search_cell);
                        cell = search_cell;
                    }
                    else
                    {
                        // 查找不到则 增加 或 更新
                        if (lv_obj_get_child_cnt(obj) < collection->count)
                        {
                            // 现有cell不足则增加
                            cell = add_midea_collection_item_ui(collection, index, current, false);
                        }
                        // 更新cell child使ui_type一致
                        reinstall_midea_collection_item_ui(collection, cell, index, current);
                    }
                }
            }
            // 刷新
            collection->update_collection(current->data, cell, index, current->ui_type);
        }
        index += 1;
        if (index > end_index)
        {
            return;
        }
        current = current->next;
    }
    // 如果在index 和 end_index 之间存在多余的UI item
    // 则删除
    if (index <= end_index && lv_obj_get_child_cnt(obj) > index)
    {
        for (int32_t i = index; i <= end_index; i++)
        {
            lv_obj_t *more_cell = lv_obj_get_child(obj, index);
            if (more_cell == NULL)
            {
                break;
            }
            debug_d("del\n");
            lv_obj_del(more_cell);
        }
    }
}